
$(function() {
	$("input[id^='code_s']:visible").autocomplete(Test.mold_code_my);
	
	$("input[id^='customer_s']:visible").autocomplete(Test.customer);

	$("input[id^='engineer_s']:visible").autocomplete(Test.emp);
	
	_ck_default("input[id^='engineer']:visible", "input[id^='engineer_t']:visible");

	Tip.emp('业务员');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("业务员：");
		} else if (a == 'oper') {
			label.html("录入员：");
		}
	});
	
	Tip.supplier();
});

var MoldMy = {

	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {			
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_add")[0];
	
				var body = { };
				body.code = form.code.value;
				body.category = form.category.value;
				body.date = form.date.value;
				body.content = form.content.value;
	
				$(this).dialog("close");
	
				var url = 'mold.php?do=MoldMy.manualPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	popup: function (e, p2, id, status, outer) {
		if (outer == 1) {
			return this.aa(e, p2, id, status);
		}

		return this.ab(e, p2, id, status);
	},

	/**
	 * 景荣制造
	 */
	aa: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'mold.php?do=MoldMy.edit&id=' + id + p2;
		var c2 = 'mold.php?do=MoldMy.view&id=' + id + p2;
		
		var cf = 'mold.php?do=MoldMy.status&id=' + id + p2;
		
		var c5 = 'mold.php?do=MoldMy.work&id=' + id;

		var c7 = 'mold.php?do=Plan.findPost&mold_s=' + id;
		var c8 = 'mold.php?do=Plan.report&id=' + id;

		var a1 = ['编辑信息', 'url', 'MoldMy.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];
		
		var a5 = ['跟模信息', 'url', 'MoldMy.dialog_work', '650x420'];
		var a7 = ['生产计划', 'url', 'list'];
		var a8 = ['产品生产', 'url', 'jswin', '1024x800'];
		
		if (status == 1) {
			var s = c1 + ", " + c2;

			return this.popup_(e, [a1, a2], s);
		}

		if (status > 1 && status < 9) {
			var s = c1 + ", " + c2 + ", " + c5 + ", " + c8;

			return this.popup_(e, [a1, a2, a5, a8], s);
		}

		if (status == 9) {
			var s = c2 + ", " + c5 + ", " + c8;

			return this.popup_(e, [a2, a5, a8], s);
		}

		var s = c2 + ", " + c5 + ", " + c8;

		return this.popup_(e, [a2, a5, a8], s);
	},

	/**
	 * 外发制造
	 */
	ab: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'mold.php?do=MoldMy.edit&id=' + id + p2;
		var c2 = 'mold.php?do=MoldMy.view&id=' + id + p2;
		
		var cf = 'mold.php?do=MoldMy.status&id=' + id + p2;
		
		var c5 = 'mold.php?do=MoldMy.work&id=' + id;

		var a1 = ['编辑信息', 'url', 'MoldMy.dialog_edit_', '600x360', '编辑信息'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '600x360', '查看信息'];
		
		var a5 = ['跟模信息', 'url', 'MoldMy.dialog_work', '650x420', '跟模信息'];

		if (status == 1) {
			var s = c1 + ", " + c2;

			return this.popup_(e, [a1, a2], s);
		}

		if (status > 1 && status < 9) {
			var s = c1 + ", " + c2 + ", " + c5;

			return this.popup_(e, [a1, a2, a5], s);
		}

		if (status == 9) {
			var s = c2 + ", " + c5;

			return this.popup_(e, [a2, a5], s);
		}

		var s = c2 + ", " + c5;

		return this.popup_(e, [a2, a5], s);
	},
	
	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var c2= 'mold.php?do=MoldMy.upload&id=' + id;
		var a2 = ['上传附件', 'url', 'Common.dialog_upload', '600x420'];
		
		var ss = 'mold.php?do=MoldMy.ss&id=' + id;
		var bb = ['修改状态', 'url', 'MoldMy.dialog_ss', '400x200'];
		
		s += ", " + c2 + ", " + ss;
		
		a.push(a2);
		a.push(bb);
		
		return popup(e, a, s);
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=MoldMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						
						body.customer = form.customer.value;
						body.code = form.code.value;
						body.category = form.category.value;
						body.date = form.date.value;
						
						body.supplier = form.supplier.value;
						body.supplier_x = Supplier.supplier_x(form);
						
						body.content = form.content.value;
												
						body.engineer = form.engineer.value;
						body.emp = form.emp.value;
						
						body.status = form.status.value;

						$(this).dialog("close");
						
						var url = 'mold.php?do=MoldMy.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	download: function (key) {
		if (!confirm("确认是否下载？若下载，建议先另存，再打开。")) {
			return false;
		}
		
		var url = 'mold.php?do=MoldMy.download&key=' + key;
		jswin(url, 'save', '800x600');
		
		return true;
	},
	
	file: function (id, key) {
		if (!confirm("确认是否删除？")) {
			return false;
		}
		
		var c6 = 'mold.php?do=MoldMy.upload&id=' + id;
		
		var url = 'mold.php?do=Plan.file';
		var body = { };
		body.key = key;
		
		$.post(url, body, function(data) {
			// alert(data);
			$.get(c6, function(data) {
				$('#dialog_edit').html(data);
			});
		});
		
		return true;
	},
	
	dialog_work: function (url, title, xy) {
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"提交": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				body.id = form.id.value;
				
				body.work = data;

				$(this).dialog("close");
				
				var url = 'mold.php?do=MoldMy.workPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	dialog_add_plan: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				body.id = form.id.value;
				body.piece = form.piece.value;
				body.technics = form.technics.value;
				body.emp = form.emp.value;
				body.content = form.content.value;

				body.t1 = form.t1.value;
				body.t2 = form.t2.value;

				body.hour = form.hour.value;
				body.minute = form.minute.value;

				$(this).dialog("close");
				
				var url = 'mold.php?do=Plan.addPost';

				$.post(url, body, function(data) {
					alert("添加成功！");
				});
			}
		});

		dialog_add(url, title, xy);
	},

	list_end: function (url, view) {
		$.getJSON('mold.php?do=MoldMy.ajax_engineer', function(json) {
			if (json.status == 1) {
				if (confirm("是否要完成生产？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	list_test: function (url, view) {
		$.getJSON('mold.php?do=MoldMy.ajax_engineer', function(json) {
			if (json.status == 1) {
				if (confirm("是否要开始试模？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	list_test_end: function (url, view) {
		$.getJSON('mold.php?do=MoldMy.ajax_engineer', function(json) {
			if (json.status == 1) {
				if (confirm("是否要完成试模？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	dialog_info: function (url, title, xy) {
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.id = form.id.value;
				body.date = form.date.value;
				body.content = form.content.value;

				var url = 'mold.php?do=Info.addPost';

				$.post(url, body, function(data) {
					var a = $.evalJSON(data);

					var s = "mold.php?do=Info.ajax_upload&id=" + a.id + "&name=" + $("#filename").val();
					$('#userfile').uploadifySettings('script', s);

					if ($("#_upload").val() == "1") {
						$("#dialog_add").dialog("close");
					} else {
						$('#userfile').uploadifyUpload();
					}
				});
			}
		});

		dialog_add(url, title, xy);
	}
};
